package com.runfast.waimai.web.controller.shopper;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastCreditlevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION} 骑手等级管理
 *
 * @author chenzheng
 * @create 2018-05-21 16:52
 **/
@RestController("shopperCreditLevelController")
@RequestMapping("/server/shopperCreditLevel")
public class ShopperCreditLevelController {
    @Autowired
    private RunfastCreditlevelService runfastCreditlevelService;
    /**
     * 骑手等级列表带分页的
     */
    @GetMapping("/list")
    public Result list(@PageableDefault Pageable pageable, Map<String, Object> params){
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        RunfastBusinessAccounts accounts = SessionContext.getRunfastBusinessAccounts();
        RunfastCreditlevelExample example=new RunfastCreditlevelExample();
        if(agentAccounts!=null){
            example.createCriteria().andAgentidIn(agentAccounts.getAgents());
        }else if(admin.getType()==1){
            if(admin.getAgents().size()>0){
                example.createCriteria().andAgentidIn(admin.getAgents());
            }else{
                example=null;
            }
        }
        List<RunfastCreditlevel> list=runfastCreditlevelService.selectByExampleWithPageable(example,pageable);
        Integer total=runfastCreditlevelService.countByExample(example);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /**
     * 骑手等级列表
     */
   /* @GetMapping("/select")
    //@RequiresPermissions("server:goodsSellType:select")
    public Result select(){
        List<RunfastGoodsSellType> list = runfastGoodsSellTypeService.selectByExample(null);
        return Result.ok(null,list);
    }*/

    /**
     * 保存骑手等级
     */
    @PostMapping("/save")
    public Result save(RunfastCreditlevel t){
        t.setCreatetime(new Date());
        runfastCreditlevelService.insertSelective(t);
        return Result.ok(null);
    }

    /**
     * 修改骑手等级
     */
    @PostMapping("/update")
    public Result update(RunfastCreditlevel t){
        if(t==null||t.getId()==null){return Result.fail(null,"骑手等级信息不能为空！");}
        runfastCreditlevelService.updateByPrimaryKeySelective(t);
        return Result.ok(null);
    }

    /**
     * 删除骑手等级--暂时不实现
     */
    /*@PostMapping("/delete")
    public R delete(@RequestBody Long[] roleIds){
        sysRoleService.deleteBatch(roleIds);

        return R.ok();
    }*/

}
